package com.school.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import com.school.entity.HljtDuty;
import com.school.entity.HljtSchoolimage;
import com.school.entity.web.BasicsInfo;
import com.school.entity.web.HljtSchoolSynopsis;
import com.school.service.impl.HljtSchoolManagerServiceImpl;
import com.school.util.FileUploadUtil;
import com.school.util.PictureVideoUpload;
import com.school.util.RepUtil;
import com.school.util.WhetherNotToUpdate;

@Controller
@RequestMapping(value="SchoolManager")
public class HljtSchoolManagerController {
	@Autowired
	HljtSchoolManagerServiceImpl oHljtSchoolManagerServiceImpl;
	
	/**
	 * 
	 * 根据类型查询 学校图片信息
	 * @param nType 1 学校简介   4 学校校徽
	 * @param rep
	 * @param req
	 * @return
	 */
	
	@RequestMapping(value="/findImageInfo")
	@ResponseBody
	public String findImageInfo(int nType,HttpServletResponse rep,HttpServletRequest req){
		RepUtil.repPake(rep);
	//	String strPath=RepUtil.findAddress(req);
		return oHljtSchoolManagerServiceImpl.findImageInfo(nType);
	}
	
	/**
	 * 保存校园简介
	 * @param imgFile
	 * @param oHljtSchoolSynopsis
	 * @param rep
	 * @param req
	 * @return
	 * @throws IOException
	 */
  	@RequestMapping(value ="/saveSchoolInfo",method = RequestMethod.POST)
  	@ResponseBody
  	    public String saveSchoolInfo(HljtSchoolSynopsis oHljtSchoolSynopsis,HttpServletResponse rep ,HttpServletRequest req) throws IOException {
  		RepUtil.repPake(rep);
  		
  		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
 		 MultipartFile file = null;
 		String imagePath = null;
 		if (isMultipart){ 
		    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(req, MultipartHttpServletRequest.class);
		     file = multipartRequest.getFile("imgFile");
		     if(!file.isEmpty()) {
			     imagePath = PictureVideoUpload.savePictureVideo(file, WhetherNotToUpdate.getSaveDisk());
			     oHljtSchoolSynopsis.setImagePath(imagePath);
		    }
		}
		return  oHljtSchoolManagerServiceImpl.saveSchoolInfo(oHljtSchoolSynopsis);
  	}
  	
  	/**
  	 * 保存图片信息
  	 * @param imgFile
  	 * @param oHljtSchoolimage
  	 * @param rep
  	 * @param req
  	 * @return
  	 * @throws IOException
  	 */
  	@RequestMapping(value ="/saveImageInfo",method = RequestMethod.POST)
  	@ResponseBody
  	    public String saveImageInfo( HljtSchoolimage oHljtSchoolimage,HttpServletResponse rep,HttpServletRequest req) throws IOException {
  		RepUtil.repPake(rep);
  	//	String ProjectPath=this.getClass().getClassLoader().getResource("/").getPath() ; 
  	//	oHljtSchoolimage.setImagepath(FileUploadUtil.saveFileInfo(imgFile,ProjectPath));
  		
  		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
  		
		 MultipartFile file = null;
		String imagePath = null;
		if (isMultipart){ 
		    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(req, MultipartHttpServletRequest.class);
		     file = multipartRequest.getFile("imgFile");
		     if(!file.isEmpty()) {
			     imagePath = PictureVideoUpload.savePictureVideo(file, WhetherNotToUpdate.getSaveDisk());
			     oHljtSchoolimage.setImagepath(imagePath);
		    }
		}
		
		return  oHljtSchoolManagerServiceImpl.saveImageInfo(oHljtSchoolimage);
  	}
  	
  	/**
  	 * 查找所有德育设置信息
  	 * @param rep
  	 * @param req
  	 * @return
  	 */
  	@RequestMapping(value="/findEducationSet")
	@ResponseBody
	public String findEducationSet(int pageIndex,int pageSize,HttpServletResponse rep,HttpServletRequest req){
		RepUtil.repPake(rep);
		String strPath=RepUtil.findAddress(req);
		return oHljtSchoolManagerServiceImpl.findAllHljtDutyInfo(strPath,pageIndex,pageSize);
	}
  	/**
  	 * 添加德育设置信息
  	 * @param rep
  	 * @param req
  	 * @return
  	 */
  	@RequestMapping(value ="/insertEducationSet",method = RequestMethod.POST)
  	@ResponseBody
  	    public String insertEducationSet(@RequestParam MultipartFile imgFile, HljtDuty oHljtDuty, HttpServletResponse rep,HttpServletRequest req) throws IOException {
  		RepUtil.repPake(rep);
  		String ProjectPath=this.getClass().getClassLoader().getResource("/").getPath() ; 
  		oHljtDuty.setDutyimage(FileUploadUtil.saveFileInfo(imgFile,ProjectPath));
		return null;
  	}
  	/**
  	 * 查找所有德育设置信息
  	 * @param rep
  	 * @param req
  	 * @return
  	 */
  	@RequestMapping(value ="/updateEducationSet",method = RequestMethod.POST)
  	@ResponseBody
  	    public String updateEducationSet(@RequestParam MultipartFile imgFile,HljtDuty oHljtDuty,  HttpServletResponse rep,HttpServletRequest req) throws IOException {
  		RepUtil.repPake(rep);
  		String ProjectPath=this.getClass().getClassLoader().getResource("/").getPath() ; 
  		oHljtDuty.setDutyimage(FileUploadUtil.saveFileInfo(imgFile,ProjectPath));
		return null;
  	}
  	/**
  	 * 查找所有德育设置信息
  	 * @param rep
  	 * @param req
  	 * @return
  	 */
  	@RequestMapping(value="/")
	@ResponseBody
	public String findEducationSet(int id,HttpServletResponse rep){
		RepUtil.repPake(rep);
		return null;
	}
  	
}
